import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../../common/colors.dart';

class CreateTeamPage extends StatefulWidget {
  const CreateTeamPage({Key? key}) : super(key: key);

  @override
  State<CreateTeamPage> createState() => _CreateTeamPageState();
}

class _CreateTeamPageState extends State<CreateTeamPage> {
  
  
  void requestDataWithCreateTeam(var teamName)async{

    var params = {
      'name':teamName
    };
    var json = await DioManager().kkRequest(Address.teamCreate,bodyParams: params,isShowLoad: true);
    if(json['errmsg']=='ok'){
      Get.back(result: 'refresh');
    }

    BotToast.showText(text: json['message']);
  }

  TextEditingController _editingController = TextEditingController();
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: AppColor.themeColor,
        leading: IconButton(
          onPressed: () {
            Get.back();
          },
          icon: Image.asset(
            'images/leftarr.png',
            width: 15,
            height: 15,
            color: Colors.white,
          ),
        ),
        title: Text('创建团队',style: TextStyle(color: Colors.white),),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(top: 50,left: 25),
            child: Text('创建团队',style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold),),
          ),

          Container(
            margin: EdgeInsets.only(left: 25,right: 25),
            child: TextField(
              autofocus: true,
              controller: _editingController,
              inputFormatters: <TextInputFormatter>[
                LengthLimitingTextInputFormatter(20) //限制长度
              ],
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: '请输入团队名称',
                hintStyle: TextStyle(fontSize: 14)
              ),
            ),
          ),

          Center(
            child:MaterialButton(onPressed: (){
              if(_editingController.text.isEmpty){
                BotToast.showText(text: '请输入团队的名称');
                return;
              }
              requestDataWithCreateTeam(_editingController.text);
            },child: Text('完成',style: TextStyle(color: Colors.white),),color: AppColor.themeColor,
            minWidth: Get.width-50,
            ),
          )
        ],
      ),
    );
  }
}
